/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.controllers
{
	import com.puny.mvc.models.Model;
	import com.puny.mvc.views.ComboBoxView;
	
	import flash.events.Event;

	/**
	 * This controller is for a ComboBox. In this framework, a combo box consits of 4 exported <code>MovieClip</code>, <code>List</code>, <code>Slider</code>, <code>Thumb</code>, and <code>TextForList</code>.
	 * To populate values in a list, an xml is required.
	 * 
	 * @author puny
	 * @example
	 * This is a sample XML. It should have a width and values.
	 * <listing version="3.0"> 
	 *	&lt;combox width="173"&gt;
	 *		&lt;value&gt;&lt;![CDATA[Goerge Washington]]&gt;&lt;/value&gt;
	 *		&lt;value&gt;&lt;![CDATA[John Adams]]&gt;&lt;/value&gt;
	 *		&lt;value&gt;&lt;![CDATA[Thomas Jefferson]]&gt;&lt;/value&gt;
	 *		&lt;value&gt;&lt;![CDATA[James Madison]]&gt;&lt;/value&gt;
	 *		&lt;value&gt;&lt;![CDATA[James Monroe]]&gt;&lt;/value&gt;
	 *		&lt;value&gt;&lt;![CDATA[John Quincy Adams]]&gt;&lt;/value&gt;
	 *		&lt;value&gt;&lt;![CDATA[Andrew Jackson]]&gt;&lt;/value&gt;
	 *		&lt;value&gt;&lt;![CDATA[Martin Van Buren]]&gt;&lt;/value&gt;
	 *	&lt;/combox&gt;
	 * </listing>
	 * In the sample below, assume the given xml should be something like the above.
	 * <listing version="3.0">
	 * var dobj:DisplayObject = addChild(new ComboBox(xml, "MyAssets.swf"));
	 * dobj.addEventListener(ComboBox.SELECTED, selectedHandler);
	 * 
	 * private function selectedHandler(event:Event):void {
	 * 	trace(event.currentTarget.id, event.currentTerget[ComboBox.SELECTED]);
	 * }
	 * </listing>
	 */
	public class ComboBox extends Controller
	{
		public static const SELECTED:String = "selected";
		private var _assets:Object;
		private var _view:ComboBoxView;
		private var _model:Model;
		private var _selectedText:String;
		private var _xml:XML;
		public function ComboBox(config:XML,assets:Object)
		{
			_assets = assets;
			_xml = config;
			super();
		}
		
		override protected function setup():void {
			_model = new Model();
			_model.addEventListener(SELECTED, selectedHandler);
			_view = new ComboBoxView(_assets,_xml,_model);
//			_view.addItems(_xml);
//			_view.model = _model;
			_assets = null;
		}
		
		override public function beforeReady():void {
			addChild(_view);
		}
				
		private function selectedHandler(event:Event):void {
			_selectedText = event.currentTarget.selected;
//			trace(event.currentTarget.id, event.currentTarget.selected);
			dispatchEvent(event.clone());
		}
		
		public function set selectedText(value:String):void {
			_model.selected = value;
		}
		
		public function get selectedText():String {
			return _selectedText;
		}
	}
}